package com.kmzx.one.project.controller;

import com.kmzx.one.common.utils.MyAssert;
import com.kmzx.one.project.net.BaseResult;
import com.kmzx.one.project.others.common.CashResult;
import com.kmzx.one.project.others.dto.FreightDetailInfo;
import com.kmzx.one.project.others.dto.FreightInfo;
import com.kmzx.one.project.others.dto.UserCashInfo;
import com.kmzx.one.project.others.dto.UserProfitDetailInfo;
import com.kmzx.one.project.others.dto.UserProfitInfo;
import com.kmzx.one.project.service.UserCashService;
import com.kmzx.one.project.service.UserProfitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@Api(tags = "快美甄选_[ 用户_收益_接口 ]")
@RestController
public class UserProfitController extends BaseController {


    @Autowired
    private UserProfitService userProfitService;

    @Autowired
    UserCashService userCashService;

    /**
     * 我的总收益
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "1.我的总收益", notes = "我的总收益")
    @GetMapping(value = "profit/entry")
    @ApiImplicitParam(name = "userId", value = "useId", required = true)
    public BaseResult profitEntry(@RequestParam("userId") String userId) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            UserProfitInfo info = userProfitService.findUserProfit(userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    /**
     * 提现明细
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "2.收入/提现明细", notes = "收入/提现明细")
    @GetMapping(value = "profit/inOrOut")
    @ApiImplicitParam(name = "userId", value = "useId", required = true)
    public BaseResult profitOutcome(@RequestParam("userId") String userId,
                                    @RequestParam("pageNum") Integer pageNum,
                                    @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam("type") Integer type) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            MyAssert.notNullObject(pageNum, "pageNum 为null");
            MyAssert.notNullObject(pageSize, "pageSize 为null");
            List<UserProfitDetailInfo> info = userProfitService.findUserInOrOutcomeMore(userId, pageNum, pageSize, type);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "3.申请提现", notes = "申请提现")
    @GetMapping(value = "profit/cash")
    public BaseResult userCashBack(String userId, BigDecimal money) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            MyAssert.notNullObject(money, "money 为null");
            BaseResult fail = BaseResult.Builder.FAIL();
            CashResult info = userCashService.doWithdraw(userId, money);
            if (info.isSuccess()) {
                fail = BaseResult.Builder.SUCC();
                fail.setData(info.getCashId());
                fail.setErrMsg("提交审核中...");
            } else {
                fail.setErrMsg("请正确操作!");
                fail.setErrMsg(info.getReason());
            }
            return fail;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    @ApiOperation(value = "4.提现 结果", notes = "提现 结果")
    @GetMapping(value = "profit/cashInfo")
    public BaseResult userCashInfo(String userId, String cashId) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            MyAssert.notNullObject(cashId, "cashId 为null");
            UserCashInfo info = userProfitService.findUserCashInfo(userId, cashId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    @ApiOperation(value = "5.已读消息", notes = "已读消息")
    @GetMapping(value = "profit/read")
    public BaseResult userCashRead(String cashId) {
        try {
            MyAssert.notNullObject(cashId, "userId 为null");
            int info = userProfitService.doReadCash(cashId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    @ApiOperation(value = "6.我的货仓", notes = "我的货仓",position = -1)
    @GetMapping(value = "profit/freight")
    public BaseResult myFreight(String userId) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            FreightInfo info = userProfitService.findUserFreight(userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "7.我的货仓,补差价", notes = "我的货仓,补差价",position = -1)
    @GetMapping(value = "profit/freightAppend")
    public BaseResult appendFreight(String userId, BigDecimal money) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            MyAssert.notNullObject(money, "money 为null");
            Object info = userProfitService.doBuyUserRanks(userId, money);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "8.我的货仓,下拉刷新", notes = "我的货仓,下拉刷新",position = -1)
    @GetMapping(value = "profit/freightMore")
    public BaseResult freightDetail(String userId, Integer pageNum, Integer pageSize) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            MyAssert.notNullObject(pageNum, "pageNum 为null");
            MyAssert.notNullObject(pageSize, "pageSize 为null");
            List<FreightDetailInfo> info = userProfitService.findUserFreightPager(userId, pageNum, pageSize);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "9.我已经提现了的金额", notes = "我已经提现了的金额",position = -1)
    @GetMapping(value = "profit/hadOut")
    public BaseResult findMyBeforeWithdraw(String userId) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            BigDecimal withdraw = userProfitService.myEverWithdraw(userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(withdraw);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    @ApiOperation(value = "10.是否是提现日", notes = "是否是提现日",position = -1)
    @GetMapping(value = "profit/allow")
    public BaseResult checkWithdrawAllow() {
        try {
            int withdraw = userProfitService.checkWithdrawAllow();
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(withdraw);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    @ApiOperation(value = "11.取消提现", notes = "取消提现",position = -1)
    @GetMapping(value = "profit/cancel")
    public BaseResult cancelWithdraw(String userId, String cashId) {
        try {
            int withdraw = userCashService.cancelWithdraw(userId, cashId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(withdraw);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "12.收益说明", notes = "收益说明")
    @GetMapping(value = "profit/detailInfo")
    public BaseResult getProfitMsg(String userId, String tempId) {
        try {
            String withdraw = userProfitService.findUserProfitMsg(userId, tempId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(withdraw);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }
}
